package com.guli.gulimall.product.web.controller;

import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;
import com.guli.gulimall.product.service.SkuInfoService;
import com.guli.gulimall.product.web.entity.vo.Catalog2Vo;
import com.guli.gulimall.product.web.entity.vo.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @ClassName IndexController
 * @Author Hao_YunKai
 * @Date 2021/12/28 21:33
 */

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SkuInfoService skuInfoService;

    @RequestMapping(value = {"/","index.html"})
    public String index(Model model) {
        // 访问首页查询一级分类
        List<CategoryEntity> list = categoryService.find1List();

        model.addAttribute("list",list);
        return "index";
    }

    @RequestMapping("/findCatalog")
    @ResponseBody
    public Map<String ,List<Catalog2Vo>> findCatalog() {
        return categoryService.findCatalogRedisCache();
    }

    /* 详情页面 */
    @GetMapping("/{skuId}.html")
    public String item(@PathVariable("skuId") Long skuId, Model model) {
        SkuItemVo item = skuInfoService.getItemBySkuId(skuId);
        model.addAttribute("item",item);
        return "item";
    }

}